package de.se.ludoapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import de.se.ludoapp.LudoApplication;
import de.se.ludoapp.R;
import de.se.ludoapp.activity.task.RegisterTask;
import de.se.ludoapp.gaming.LudoSystemStub;

/**
 * Login Activity (Startpage)
 * 
 * @author L.Rother
 * 
 */
public class RegistrationActivity extends Activity {

	private LudoApplication ludoApp;
	private SharedPreferences prefs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Initialise Stub LudoServer
		ludoApp = (LudoApplication) this.getApplication();
		ludoApp.setLudoStub(new LudoSystemStub());

		// View
		setContentView(R.layout.register);

		// This is the link to change to the Registration Screen
		TextView registerScreen = (TextView) findViewById(R.id.link_to_login);
		registerScreen.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// Switching to Register screen
				Intent i = new Intent(getApplicationContext(),
						LoginRememberActivity.class);
				startActivity(i);
			}
		});

		// Login Button
		Button buttinRegister = (Button) findViewById(R.id.btnRegister);
		buttinRegister.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText editTextUsername = (EditText) findViewById(R.id.usernameRegInput);
				String userName = editTextUsername.getText().toString();
				EditText editTextPassword = (EditText) findViewById(R.id.pwdRegInput);
				String pwd = editTextPassword.getText().toString();
				EditText editTextSndPwd = (EditText) findViewById(R.id.pwdScndRegInput);
				String sndPwd = editTextSndPwd.getText().toString();

				if (userName.trim().length() == 0 || pwd.trim().length() == 0
						|| sndPwd.trim().length() == 0) {
					Toast.makeText(RegistrationActivity.this,
							"Ueberpruefen Sie ihre Eingaben", Toast.LENGTH_LONG)
							.show();
				} else if (!pwd.equals(sndPwd)) {
					Toast.makeText(RegistrationActivity.this,
							"Bitte verwenden Sie identische Passwoerter",
							Toast.LENGTH_LONG).show();
				} else {
					new RegisterTask(ludoApp, prefs, RegistrationActivity.this,
							userName, pwd, sndPwd).execute();
					Button bnLogin = (Button) findViewById(R.id.btnRegister);
					bnLogin.setEnabled(false);
				}

			}

		});

		prefs = PreferenceManager.getDefaultSharedPreferences(this);
	}

}
